package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.CommonTrendChartReq;
import com.ruibang.glass.produce.domain.req.MaterialParticleReq;
import com.ruibang.glass.produce.domain.resp.MaterialParticleTrendChart;
import com.ruibang.glass.produce.entity.MaterialParticle;
import com.ruibang.glass.produce.mapper.MaterialParticleMapper;
import com.ruibang.glass.produce.service.MaterialParticleService;
import com.ruibang.glass.produce.util.DataUtil;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
//import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 生产-理化-原料粒度 服务实现类
 * </p>
 *
 * @author songJinKang
 * @since 2024-03-12
 */
@Slf4j
@Service
public class MaterialParticleServiceImpl extends ServiceImpl<MaterialParticleMapper, MaterialParticle> implements MaterialParticleService {

    @Autowired
    private RedisSingleService redisService;
//    @PostConstruct
//    public void initData() {
//        List<MaterialParticle> list = this.list();
//        for (MaterialParticle moistureDeteciton : list) {
//            String auditById = moistureDeteciton.getAuditById();
//            if (StringUtils.isNotBlank(auditById)) {
//                Object auditObj = redisService.hget(RedisKey.USER_GROUP_TYPE, auditById);
//                if (auditObj != null) {
//                    moistureDeteciton.setAuditBy(auditObj.toString());
//                }
//            }
//            String checkById = moistureDeteciton.getCheckById();
//            if (StringUtils.isNotBlank(checkById)) {
//                Object checkObj = redisService.hget(RedisKey.USER_GROUP_TYPE, checkById);
//                if (checkObj != null) {
//                    moistureDeteciton.setCheckBy(checkObj.toString());
//                }
//            }
//        }
//        updateBatchById(list);
//    }

    @Override
    public IPage<MaterialParticle> pageQuery(MaterialParticleReq materialParticleReq) {
        QueryWrapper<MaterialParticle> qw = new QueryWrapper<>();
        qw.lambda().eq(MaterialParticle::getIsDelete, Constants.ZERO_STR);
        if (materialParticleReq.getStartTime() != null && materialParticleReq.getEndTime() != null) {
            qw.lambda().ge(MaterialParticle::getArrivalTime, materialParticleReq.getStartTime());
            qw.lambda().le(MaterialParticle::getArrivalTime, materialParticleReq.getEndTime());
        }
        qw.lambda().orderByDesc(MaterialParticle::getCreateTime);
        return this.page(new Page<>(materialParticleReq.getPageNo(), materialParticleReq.getPageSize()), qw.lambda());
    }

    @Override
    public void delete(String id) {
        lambdaUpdate().eq(MaterialParticle::getId, id).set(MaterialParticle::getIsDelete, Constants.ONE_STR).update();
    }

    @Override
    public MaterialParticleTrendChart trendChart(CommonTrendChartReq trendChartReq) {
        //默认查询3天
        if (trendChartReq.getStartTime() == null || trendChartReq.getEndTime() == null) {
            LocalDateTime now = LocalDateTime.now();
            trendChartReq.setStartTime(now.minusDays(3));
            trendChartReq.setEndTime(now);
        }
        //组合数据
        MaterialParticleTrendChart materialParticleTrendChart = new MaterialParticleTrendChart();
        List<MaterialParticle> data = baseMapper.trendChart(trendChartReq);
        if (CollectionUtils.isNotEmpty(data)) {
            //处理数据
            for (MaterialParticle datum : data) {
                processData(datum);
            }
            //填充数据
            materialParticleTrendChart.setA1(data.stream().map(MaterialParticle::getA1).collect(Collectors.toList()));
            materialParticleTrendChart.setA2(data.stream().map(MaterialParticle::getA2).collect(Collectors.toList()));
            materialParticleTrendChart.setA3(data.stream().map(MaterialParticle::getA3).collect(Collectors.toList()));
            materialParticleTrendChart.setB1(data.stream().map(MaterialParticle::getB1).collect(Collectors.toList()));
            materialParticleTrendChart.setB2(data.stream().map(MaterialParticle::getB2).collect(Collectors.toList()));
            materialParticleTrendChart.setB3(data.stream().map(MaterialParticle::getB3).collect(Collectors.toList()));
            materialParticleTrendChart.setC1(data.stream().map(MaterialParticle::getC1).collect(Collectors.toList()));
            materialParticleTrendChart.setC2(data.stream().map(MaterialParticle::getC2).collect(Collectors.toList()));
            materialParticleTrendChart.setD1(data.stream().map(MaterialParticle::getD1).collect(Collectors.toList()));
            materialParticleTrendChart.setD2(data.stream().map(MaterialParticle::getD2).collect(Collectors.toList()));
            materialParticleTrendChart.setD3(data.stream().map(MaterialParticle::getD3).collect(Collectors.toList()));
            materialParticleTrendChart.setE1(data.stream().map(MaterialParticle::getE1).collect(Collectors.toList()));
            materialParticleTrendChart.setE2(data.stream().map(MaterialParticle::getE2).collect(Collectors.toList()));
            materialParticleTrendChart.setF1(data.stream().map(MaterialParticle::getF1).collect(Collectors.toList()));
            materialParticleTrendChart.setF2(data.stream().map(MaterialParticle::getF2).collect(Collectors.toList()));
            materialParticleTrendChart.setF3(data.stream().map(MaterialParticle::getF3).collect(Collectors.toList()));
            materialParticleTrendChart.setG1(data.stream().map(MaterialParticle::getG1).collect(Collectors.toList()));
            materialParticleTrendChart.setG2(data.stream().map(MaterialParticle::getG2).collect(Collectors.toList()));
            materialParticleTrendChart.setH1(data.stream().map(MaterialParticle::getH1).collect(Collectors.toList()));
            materialParticleTrendChart.setH2(data.stream().map(MaterialParticle::getH2).collect(Collectors.toList()));
            materialParticleTrendChart.setCollectTime(data.stream().map(e -> e.getArrivalTime().format(Constants.DATE_TIME_FORMATTER)).collect(Collectors.toList()));
        }
        return materialParticleTrendChart;
    }

    @Override
    public void export(MaterialParticleReq materialParticleReq, HttpServletResponse response) {
        materialParticleReq.setPageNo(-1);
        materialParticleReq.setPageSize(-1);
        IPage<MaterialParticle> page = this.pageQuery(materialParticleReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<MaterialParticle> materialParticleList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), MaterialParticle.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(materialParticleList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void saveInfo(MaterialParticle materialParticle) {
        materialParticle.setIsDelete(Constants.ZERO_STR);
        setUserName(materialParticle);
        this.save(materialParticle);
    }

    @Override
    public void edit(MaterialParticle materialParticle) {
        setUserName(materialParticle);
        this.updateById(materialParticle);
    }

    private void setUserName(MaterialParticle materialParticle) {
        String auditById = materialParticle.getAuditById();
        String checkById = materialParticle.getCheckById();
        if (StringUtils.isNotEmpty(checkById)) {
            Object userObj = redisService.hget(RedisKey.USER_GROUP_TYPE, checkById);
            if (userObj != null && StringUtils.isNotBlank(userObj.toString())) {
                materialParticle.setCheckBy(userObj.toString());
            }
        } else {
            materialParticle.setCheckBy("");
        }
        if (StringUtils.isNotEmpty(auditById)) {
            Object userObj = redisService.hget(RedisKey.USER_GROUP_TYPE, auditById);
            if (userObj != null && StringUtils.isNotBlank(userObj.toString())) {
                materialParticle.setAuditBy(userObj.toString());
            }
        } else {
            materialParticle.setAuditBy("");
        }
    }


    private void processData(MaterialParticle datum) {
        String a1 = datum.getA1();
        if (StringUtils.isBlank(a1) || !DataUtil.isNumeric(a1)) {
            datum.setA1(Constants.ZERO_STR);
        }
        String a2 = datum.getA2();
        if (StringUtils.isBlank(a2) || !DataUtil.isNumeric(a2)) {
            datum.setA2(Constants.ZERO_STR);
        }
        String a3 = datum.getA3();
        if (StringUtils.isBlank(a3) || !DataUtil.isNumeric(a3)) {
            datum.setA3(Constants.ZERO_STR);
        }
        String b1 = datum.getB1();
        if (StringUtils.isBlank(b1) || !DataUtil.isNumeric(b1)) {
            datum.setB1(Constants.ZERO_STR);
        }
        String b2 = datum.getB2();
        if (StringUtils.isBlank(b2) || !DataUtil.isNumeric(b2)) {
            datum.setB2(Constants.ZERO_STR);
        }
        String b3 = datum.getB3();
        if (StringUtils.isBlank(b3) || !DataUtil.isNumeric(b3)) {
            datum.setB3(Constants.ZERO_STR);
        }
        String c1 = datum.getC1();
        if (StringUtils.isBlank(c1) || !DataUtil.isNumeric(c1)) {
            datum.setC1(Constants.ZERO_STR);
        }
        String c2 = datum.getC2();
        if (StringUtils.isBlank(c2) || !DataUtil.isNumeric(c2)) {
            datum.setC2(Constants.ZERO_STR);
        }
        String d1 = datum.getD1();
        if (StringUtils.isBlank(d1) || !DataUtil.isNumeric(d1)) {
            datum.setD1(Constants.ZERO_STR);
        }
        String d2 = datum.getD2();
        if (StringUtils.isBlank(d2) || !DataUtil.isNumeric(d2)) {
            datum.setD2(Constants.ZERO_STR);
        }
        String d3 = datum.getD3();
        if (StringUtils.isBlank(d3) || !DataUtil.isNumeric(d3)) {
            datum.setD3(Constants.ZERO_STR);
        }
        String e1 = datum.getE1();
        if (StringUtils.isBlank(e1) || !DataUtil.isNumeric(e1)) {
            datum.setE1(Constants.ZERO_STR);
        }
        String e2 = datum.getE2();
        if (StringUtils.isBlank(e2) || !DataUtil.isNumeric(e2)) {
            datum.setE2(Constants.ZERO_STR);
        }
        String f1 = datum.getF1();
        if (StringUtils.isBlank(f1) || !DataUtil.isNumeric(f1)) {
            datum.setF1(Constants.ZERO_STR);
        }
        String f2 = datum.getF2();
        if (StringUtils.isBlank(f2) || !DataUtil.isNumeric(f2)) {
            datum.setF2(Constants.ZERO_STR);
        }
        String f3 = datum.getF3();
        if (StringUtils.isBlank(f3) || !DataUtil.isNumeric(f3)) {
            datum.setF3(Constants.ZERO_STR);
        }
        String g1 = datum.getG1();
        if (StringUtils.isBlank(g1) || !DataUtil.isNumeric(g1)) {
            datum.setG1(Constants.ZERO_STR);
        }
        String g2 = datum.getG2();
        if (StringUtils.isBlank(g2) || !DataUtil.isNumeric(g2)) {
            datum.setG2(Constants.ZERO_STR);
        }
        String h1 = datum.getH1();
        if (StringUtils.isBlank(h1) || !DataUtil.isNumeric(h1)) {
            datum.setH1(Constants.ZERO_STR);
        }
        String h2 = datum.getH2();
        if (StringUtils.isBlank(h2) || !DataUtil.isNumeric(h2)) {
            datum.setH2(Constants.ZERO_STR);
        }
    }

}
